﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Stauffware.Common.Core;
using Stauffware.Common.Xml;
using Stauffware.Common.Xml.Xerialization;
using TO=XmlUnitTests.TestObjects;
using NUnit.Framework;

namespace XerializationUnitTests
{
	public class Xerialization_tests
	{
		//-----------------------------------------------------------------------------
		// Tests for XerialContext.IgnoreCase

		[TestCase("..\\..\\TestData\\CorrectCase.xml", false, true)]
		[TestCase("..\\..\\TestData\\CorrectCase.xml", true, true)]
		[TestCase("..\\..\\TestData\\IncorrectCase.xml", false, false)]
		[TestCase("..\\..\\TestData\\IncorrectCase.xml", true, true)]

		public static void IgnoreCase (string filePath, bool ignoreCase, bool expectSuccess)
		{
			// setup
			XerialContext context = new XerialContext(ignoreCase);
			context.DebugLog = Console.Out;
			context.TypeMappings.AddIfNeeded(Zoo_1.Zoo.XerialTypes);

			// read from XML file

			StreamReader sr = new StreamReader(filePath);

			try
			{
				Zoo_1.Zoo zoo1 = (Zoo_1.Zoo) context.ReadFromXml(sr);
				Console.WriteLine("XerialContext.ReadFromXml succeeded");
				if (!expectSuccess)
					NUnit.Framework.Assert.Fail("unexpected success");
			}
			catch (Exception x)
			{
				Console.WriteLine(ErrorUtils.DumpExceptionChain(x));
				if (expectSuccess)
					throw;
			}
			finally
			{
				sr.Close();
			}
		}

		//-----------------------------------------------------------------------------

		[TestCase("..\\..\\TestData\\CData_1.xml")]
		public static void TestCData (string filePath)
		{

			// setup

			XerialContext context = new XerialContext(true);
			context.DebugLog = Console.Out;
			context.TypeMappings.AddIfNeeded(Zoo_1.Zoo.XerialTypes);

			// read from XML file

			StreamReader sr = new StreamReader(filePath);
			Zoo_1.Zoo zoo1 = (Zoo_1.Zoo) context.ReadFromXml(sr);
			sr.Close();

			Console.WriteLine();

			// write to XML

			context.WriteToXml(zoo1, Console.Out);

			Console.WriteLine();

		}

		//-----------------------------------------------------------------------------

		[TestCase]
		public static void Test_Immutable ( )
		{
			XerialContext context = new XerialContext(true);
			context.DebugLog = Console.Out;
			context.TypeMappings.AddIfNeeded(typeof(TO.ConSct_1));

			TO.ConSct_1 data = new TO.ConSct_1(2.141M, "fjlbx");


			// write to XML

			StringWriter sw = new StringWriter();
			context.WriteToXml(data, sw);

			Console.WriteLine("\n" + sw.ToString() + "\n");

			// read from XML

			StringReader sr = new StringReader(sw.ToString());
			TO.ConSct_1 obj2 = (TO.ConSct_1) context.ReadFromXml(
									sr, null, XmlUtils.ErrorReporter(Console.Out));

			Console.WriteLine();
		}

		//-----------------------------------------------------------------------------

		[TestCase]
		public static void Test_Objects ( )
		{
			// setup

			TO.MainClass obj1 = new TO.MainClass();

			XerialContext context = new XerialContext(true);
			context.DebugLog = Console.Out;
			TO.MainClass.InitializeContext(context);

			// write to XML

			StringWriter sw = new StringWriter();
			context.WriteToXml(obj1, sw);

			Console.WriteLine("\n" + sw.ToString() + "\n");

			// read from XML

			StringReader sr = new StringReader(sw.ToString());
			TO.MainClass obj2 = (TO.MainClass) context.ReadFromXml(sr, 
											null, XmlUtils.ErrorReporter(Console.Out));

			Console.WriteLine();

			// compare old & new objects

			KellermanSoftware.CompareNetObjects.CompareObjects comparer = 
						new KellermanSoftware.CompareNetObjects.CompareObjects();
			comparer.MaxDifferences = 100;
			comparer.CompareStaticFields = false;
			comparer.CompareStaticProperties = false;

			if (comparer.Compare(obj1, obj2))
				Console.WriteLine("Old and new objects match!");
			else
			{
				Console.WriteLine(comparer.DifferencesString);
				NUnit.Framework.Assert.Fail("old and new objects don't match (see console output)");
			}
		}

		//-----------------------------------------------------------------------------

		[TestCase]
		public static void Test_Z1 ( )
		{
			// setup

			Zoo_1.Zoo zoo1 = Zoo_1.Zoo.CreateSample1();

			XerialContext context = new XerialContext(true);
			context.DebugLog = Console.Out;
			context.TypeMappings.AddIfNeeded(Zoo_1.Zoo.XerialTypes);

			// write to XML

			StringWriter sw = new StringWriter();
			context.WriteToXml(zoo1, sw);

			Console.WriteLine("\n" + sw.ToString() + "\n");

			// read from XML

			StringReader sr = new StringReader(sw.ToString());
			Zoo_1.Zoo zoo2 = (Zoo_1.Zoo) context.ReadFromXml(sr, null, XmlUtils.ErrorReporter(Console.Out));

			Console.WriteLine();

			// compare old & new objects

			KellermanSoftware.CompareNetObjects.CompareObjects comparer = 
						new KellermanSoftware.CompareNetObjects.CompareObjects();
			comparer.CompareStaticFields = false;
			comparer.CompareStaticProperties = false;

			if (comparer.Compare(zoo1, zoo2))
				Console.WriteLine("Old and new zoos match!");
			else
				Console.WriteLine(comparer.DifferencesString);

		}

		//-----------------------------------------------------------------------------

		//public static void Test_Z2 ( )
		//{
		//    const string filePath = "Test_Z2.xml";

		//    // setup

		//    Zoo_1.Zoo zoo1 = Zoo_1.Zoo.CreateSample1();

		//    XerialContext context = new XerialContext(true);
		//    context.DebugLog = Console.Out;
		//    context.TypeMappings.AddIfNeeded(Zoo_1.Zoo.XerialTypes);

		//    // write to XML

		//    StreamWriter sw = new StreamWriter(filePath);
		//    context.WriteToXml(zoo1, sw);
		//    sw.Close();

		//    // read from XML

		//    StreamReader sr = new StreamReader(filePath);
		//    Zoo_1.Zoo zoo2 = (Zoo_1.Zoo) context.ReadFromXml(sr);
		//    sr.Close();

		//    Console.WriteLine();

		//    // compare old & new objects

		//    KellermanSoftware.CompareNetObjects.CompareObjects comparer = 
		//                new KellermanSoftware.CompareNetObjects.CompareObjects();
		//    comparer.CompareStaticFields = false;
		//    comparer.CompareStaticProperties = false;

		//    if (comparer.Compare(zoo1, zoo2))
		//        Console.WriteLine("Old and new zoos match!");
		//    else Console.WriteLine(comparer.DifferencesString);

		//}

		//-----------------------------------------------------------------------------
	}





























}
